<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Car_brand extends MY_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Car_brand_model');
	}
	//查询列表
	public function index()
	{
	
		$brandname = $this->input->get_post('brandname');
		$start =  intval($this->input->get_post('per_page'));

		$params = array(
			'brandname' =>$brandname 
			,'start' => $start
			,'length'=>$this->page_size);


		$result = $this->Car_brand_model->select_page($params);
		
		$result['params'] = $params;

		//分页设置
		$base_url =  site_url("c=car_brand&m=index&brandname=$brandname");
		$result['pagelink'] = $this->create_page_links($base_url,$result['count']);
		
		$this->show_view('carBrandList',$result);
		
	}

	//编辑
	public function edit()
	{

		$this->form_validation->set_rules('brandname', '车辆品牌', 'required');

		$brandid = $this->input->get_post('brandid');

		$brandeditsubmit = $this->input->post('brandeditsubmit');
		$resultinfo = $this->Car_brand_model->select_by_id($brandid);
		$resultinfo['method'] ='edit';
		if($brandeditsubmit  && $this->form_validation->run())
		{
			 
			$brandname = $this->input->get_post('brandname');
			$brandinfo = $this->input->get_post('brandinfo');

			$result = $this->Car_brand_model->update($brandid,array('brandname'=>$brandname,'brandinfo'=>$brandinfo,'brandlogo'=>''));
			$this->show_message("编辑车辆品牌信息成功",site_url("c=car_brand&m=index"));
			
		}
		else
		{		
			
			$this->show_view('carBrandEdit',$resultinfo);
			
		}
	}
	//新增用户
	public function add()
	{
		$this->form_validation->set_rules('brandname', '车辆品牌', 'required');
		
		$brandeditsubmit = $this->input->post('brandeditsubmit');
		$resultinfo  =array('method'=>'add');
		if($brandeditsubmit  && $this->form_validation->run())
		{
			
			$brandname = $this->input->get_post('brandname');
			$brandinfo = $this->input->get_post('brandinfo');
 
			$result = $this->Car_brand_model->insert(array('brandname'=>$brandname,'brandinfo'=>$brandinfo,'brandlogo'=>''));

			$this->show_message("新增车辆品牌信息成功",site_url("c=car_brand&m=index"));
			
		}
		else
		{		
			
			$this->show_view('carBrandEdit',$resultinfo);
			
		}
	}
	//新增用户
	public function delete()
	{
		$brandid = $this->input->get_post('brandid');
		$result = $this->Car_brand_model->delete($brandid);
		
		if($result['success'])
		{
			$this->show_message("删除车辆品牌信息成功",site_url("c=car_brand&m=index"));
		}
		else
		{
			$this->show_message($result['message'],site_url("c=car_brand&m=index"));
		}

	}

 
}